package com.utilities;

import java.text.NumberFormat;
import java.util.Locale;

import com.hearwego.R;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ResultadosAdapter extends BaseAdapter{

    private Activity activity;
    private String[] dataUrlImagen,dataDist,dataNombre,dataDireccion,dataChecked;
    private static LayoutInflater inflater = null;
	public App_ImageLoader imageLoader;

    public ResultadosAdapter(Activity a, String[] urlimagen,String[] dist,String[] nombre,String[] direccion,
                    String[] checked) {
            int tam=urlimagen.length;
            dataUrlImagen=new String[tam];
            dataDist=new String[tam];
            dataNombre=new String[tam];
            dataDireccion=new String[tam];
            dataChecked=new String[tam];
            
            //inicializar falta
            activity = a;
            dataUrlImagen = urlimagen;
            dataDist=dist;
            dataNombre=nombre;
            dataDireccion=direccion;
            dataChecked=checked;
            inflater = (LayoutInflater) activity
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    		imageLoader = new App_ImageLoader(activity.getApplicationContext());

    }

    public int getCount() {
            //El tamaño es el mismo para cualquier arreglo, dataMarca o cualquier otro da lo mismo
            return dataUrlImagen.length;
    }

    public Object getItem(int position) {
            return position;
    }

    public long getItemId(int position) {
            return position;
    }

    public static class ViewHolder {
    		public ImageView imgitem;
    		public ImageView imgcheck;
            public TextView txtdist;
            public TextView txtnombre;
            public TextView txtdireccion;
            public RelativeLayout rlitem;

    }

    public View getView(final int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            ViewHolder holder;
            if (convertView == null) {
                    vi = inflater.inflate(R.layout.itemlist, null);
                    holder = new ViewHolder();
                    
                    holder.imgitem = (ImageView) vi
                    				.findViewById(R.id.img_item);
                    
                    holder.imgcheck = (ImageView) vi
                    				.findViewById(R.id.img_check);

                    holder.txtdist = (TextView) vi
                                    .findViewById(R.id.txt_dist);
                    
                    holder.txtnombre = (TextView) vi
                                    .findViewById(R.id.txt_nombre);

                    holder.txtdireccion = (TextView) vi
                                    .findViewById(R.id.txt_direccion);
                    
                    holder.rlitem = (RelativeLayout) vi
                    				.findViewById(R.id.rl_item);
                    
                    
                    vi.setTag(holder);
            } else
                    holder = (ViewHolder) vi.getTag();
            
            //Usar arreglo[position] para esto
            NumberFormat nf = NumberFormat.getInstance();
            nf = NumberFormat.getInstance(Locale.ENGLISH);
            
            if(dataUrlImagen[position].toString().equals("null")){
    			holder.imgitem.setImageResource(R.drawable.sinfoto);

    		}else{
    				imageLoader.DisplayImage("http://www.smartthinking.com.mx:8080/HearWeGo/"+dataUrlImagen[position], activity,
    				holder.imgitem);
    		}
//            holder.imgitem.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
//					Log.i("click", ""+dataNombre[position]);
//				}
//			});
            
            
//            holder.imgitem.setImageURI(Uri.parse("http://www.smartthinking.com.mx:8080/HearWeGo/"+dataUrlImagen[position]));
//            setImageResource(R.drawable.ic_launcher);
            
            holder.imgcheck.setImageResource(R.drawable.v2minicheckin2x);
            
//            holder.txtdist.setText(""+dataDist[position]);
          holder.txtdist.setText("km");

            if(dataNombre[position].toString().equals("null")){
                holder.txtnombre.setText("");
            }else{
                holder.txtnombre.setText(""+dataNombre[position]);
            }

            if(dataDireccion[position].toString().equals("null")){
                holder.txtdireccion.setText("");
            }else{
                holder.txtdireccion.setText(""+dataDireccion[position]);
            }
            
//            holder.rlitem.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
//					Toast.makeText(activity, "id "+dataNombre[position], Toast.LENGTH_LONG).show();
//				}	
//			});
            

            return vi;
    }

}
